package com.atguigu.gulimall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyRabbitConfig {

    /**
     * 修改消息发送对象时以json的方式发送，默认为java序列化的方式发送
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange stockEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        TopicExchange topicExchange = new TopicExchange("stock-event-exchange",true,false);
        return topicExchange;
    }

    @Bean
    public Queue stockReleaseStockQueue(){
        //普通队列
        Queue queue = new Queue("stock.release.stock.queue", true, false, false);
        return queue;
    }

    @Bean
    public Queue stockDelayQueue(){

        /**
         * 设置死信队列的规则
         * x-dead-letter-exchange:order-event-exchange   设置死信队列交换机
         * x-dead-letter-routing-key:order.release.order 设置死信队列路由
         * x-message-ttl:60000 设置死信队列消息存活时间
         */
        /**
         *  Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
         *  name名字
         *  durable是否持久化
         *  exclusive是否为排他队列（如果是只能允许一个连接，其他就连不上）
         *  autoDelete是否自动删除(交换机如果没有一个绑定队列，则自动删除)
         *  arguments参数
         */
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-dead-letter-exchange","stock-event-exchange");
        arguments.put("x-dead-letter-routing-key","stock.release");
        arguments.put("x-message-ttl",120000);
        Queue queue = new Queue("stock.delay.queue", true, false, false,arguments);

        return queue;
    }


    @Bean
    public Binding stockReleaseBinding(){
        /**
         * Binding(String destination, Binding.DestinationType destinationType, String exchange, String routingKey,Map<String, Object> arguments)
         * destination【目的地队列】
         * Binding.DestinationType【目的地类型】
         * exchange【交换机】
         * routingKey【路由键-->交换机与队列绑定之间的路由键】
         * arguments【自定义参数】
         */
        return  new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",null);
    }

    @Bean
    public Binding stockLockedBinding(){
        return  new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",null);
    }


    //容器创建时需要监听才会创建这些交换机、队列、绑定
//    @RabbitListener(queues = "stock.release.stock.queue")
//    public void handle(Message message){
//
//    }
}
